package com.viphuli.fragment;

import android.os.Bundle;
import android.widget.EditText;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;
import butterknife.Bind;
import butterknife.OnClick;

import com.loopj.android.http.RequestParams;
import com.offroader.utils.StringUtils;
import com.viphuli.app.R;
import com.viphuli.common.AccessTokenKeeper;
import com.viphuli.http.ApiRequest;
import com.viphuli.http.bean.PageBaseBean;

/**
 * 订单评价
 * 
 * @author li.li
 *
 */
public class PurchaseOrderCommentFragment extends BaseProgressFragment {

	@Bind(R.id.id_order_comment_total_service_score)
	protected RatingBar totalScoreText;

	@Bind(R.id.id_order_comment_service_score)
	protected RatingBar scoreText;

	@Bind(R.id.id_order_comment_profession)
	protected RatingBar professionText;

	@Bind(R.id.id_order_comment_content)
	protected EditText contentText;

	@Bind(R.id.id_comment_submit_btn)
	protected TextView submitBtn;

	private String orderId;

	@Override
	protected int obtainContentRes() {
		return R.layout.frag_order_comment;
	}

	@Override
	protected void initParams() {
		Bundle args = getArguments();
		orderId = args.getString("order_id");
	}

	@Override
	protected void initView() {
	}

	@Override
	protected void initData() {

	}
	
	@Override
	public void onRequestResonse(PageBaseBean page) {
		caller.finish();
	}

	@OnClick(R.id.id_comment_submit_btn)
	public void submit() {
		
		int scoreTotal = (int) totalScoreText.getRating();
		int scoreService = (int) scoreText.getRating();
		int scoreProfessional = (int) professionText.getRating();
		String content = contentText.getText().toString();
		
		if (scoreTotal <= 0) {
			Toast.makeText(getActivity(), getString(R.string.tip_order_comment_total_service_score), Toast.LENGTH_SHORT).show();
			return;
		}
		if (scoreService <= 0) {
			Toast.makeText(getActivity(), getString(R.string.tip_order_comment_service_score), Toast.LENGTH_SHORT).show();
			return;
		}
		if (scoreProfessional <= 0) {
			Toast.makeText(getActivity(), getString(R.string.tip_order_comment_profession), Toast.LENGTH_SHORT).show();
			return;
		}
		if (StringUtils.isBlank(content)) {
			Toast.makeText(getActivity(), getString(R.string.tip_order_comment_content), Toast.LENGTH_SHORT).show();
			return;
		}

		AccessTokenKeeper token = AccessTokenKeeper.readAccessToken(getActivity());

		if (token.isLogin()) {

			RequestParams params = new RequestParams();
			params.put("uid", token.getOpenId());
			params.put("order_id", orderId);
			params.put("content", content);
			params.put("score_total", scoreTotal);
			params.put("score_service", scoreService);
			params.put("score_professional", scoreProfessional);

			ApiRequest.purchaseOrderComment.request(this, params);

		} else {
			AccountLoginFragment.goWithConfirm(getActivity());
		}
	}

}
